package com.example.homemodule.view.ui;

import static com.example.homemodule.observable.HomeObservable.LIST_SCROLL_TO_TOP;
import static com.example.homemodule.observable.HomeObservable.REFRESH_DRUG_FRAGMENT_DATA;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.examination.mlib.constants.ARouterConstants;
import com.examination.mlib.interfaceapi.HomeBackApi;
import com.example.homemodule.R;
import com.example.homemodule.databinding.NewHomeDrugListFragmentDataBinding;
import com.example.homemodule.model.bean.HomeCommodityGroupListEntity;
import com.example.homemodule.observable.HomeObservable;
import com.example.homemodule.viewmodel.HomeDrugListViewModel;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.yilijk.base.base.BaseFragment;
import com.yilijk.base.utils.ALog;

import java.util.Observable;
import java.util.Observer;

/**
 * Created by SHICHENG
 * <p>
 * Time on
 */
public class NewHomeDrugListFragment extends BaseFragment<HomeDrugListViewModel, NewHomeDrugListFragmentDataBinding> implements XRecyclerView.LoadingListener, Observer {

    protected View baseView;
    private HomeCommodityGroupListEntity.Commodity commodity;
    private static final String ARG_COMMODITY = "arg_commodity";




    @Override
    protected int getContentViewId() {
        return R.layout.fragment_home_drug_list;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        baseView = inflater.inflate(getContentViewId(),container,false);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    protected void processLogic(Bundle bundle) {
        binding.xrvDrugList.setLoadingListener(this);
        mViewModel.initModel(binding,getContext());
        mViewModel.initView(baseView);
        binding.xrvDrugList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (dy == 0){
                    return;
                }
                HomeBackApi homeBackApi = (HomeBackApi) ARouter.getInstance().build(ARouterConstants.HomeModuleImpl).navigation();
                if (getActivity() != null) {
                    homeBackApi.hideImLayout(getActivity());
                }
            }
        });
    }

    @Override
    protected void dispostData() {

    }

    @Override
    protected void setListener() {
        mViewModel.initListener();
    }

    @Override
    public void onClick(View v) {

    }


    @Override
    public void onRefresh() {

    }

    @Override
    public void onLoadMore() {
        if (!mViewModel.isHasNext) {
            binding.xrvDrugList.loadMoreComplete();
            return;
        }
        mViewModel.page++;
        mViewModel.getCommodityList();
    }

    @Override
    public void update(Observable o, Object arg) {
        if (arg instanceof HomeObservable.ObserverBean) {
            switch (((HomeObservable.ObserverBean) arg).getType()) {
                case LIST_SCROLL_TO_TOP: {
//                    nestedScrollView.scrollTo(0, 0);
                    binding.xrvDrugList.scrollToPosition(0);
                    break;
                }
                case REFRESH_DRUG_FRAGMENT_DATA: {
                    if (getArguments()!=null){
                        commodity = (HomeCommodityGroupListEntity.Commodity) getArguments().getSerializable(ARG_COMMODITY);
                        mViewModel.refreshFragmentData(commodity);
                    }
                    break;
                }
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (getArguments()!=null){
            commodity = (HomeCommodityGroupListEntity.Commodity) getArguments().getSerializable(ARG_COMMODITY);
            mViewModel.refreshFragmentData(commodity);
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        HomeObservable.getInstance().addObserver(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        HomeObservable.getInstance().deleteObserver(this);

    }

    public String getTitle() {
        if (commodity == null) {
            if (getArguments() != null) {
                commodity = (HomeCommodityGroupListEntity.Commodity) getArguments().getSerializable(ARG_COMMODITY);
            }
        }
        return commodity != null ? commodity.getName() : "";
    }

    public static NewHomeDrugListFragment getInstance(HomeCommodityGroupListEntity.Commodity commodity) {
        NewHomeDrugListFragment fragment = new NewHomeDrugListFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(ARG_COMMODITY, commodity);
        fragment.setArguments(bundle);
        return fragment;
    }
}
